package org.jeecg.modules.polaris.postgraduate.controller;

import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.polaris.job.entity.Job;
import org.jeecg.modules.polaris.postgraduate.entity.Postgraduate;
import org.jeecg.modules.polaris.postgraduate.service.IPostgraduateService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: postgraduate
 * @Author: jeecg-boot
 * @Date:   2020-10-02
 * @Version: V1.0
 */
@Api(tags="postgraduate")
@RestController    //返回json数据
@RequestMapping("/polaris/postgraduate")
@Slf4j
public class PostgraduateController extends JeecgController<Postgraduate, IPostgraduateService> {
	@Autowired
	private IPostgraduateService postgraduateService;
	/**
	 * 分页列表查询
	 *
	 * @param postgraduate
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "postgraduate-分页列表查询")
	@ApiOperation(value="postgraduate-分页列表查询", notes="postgraduate-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(Postgraduate postgraduate,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<Postgraduate> queryWrapper = QueryGenerator.initQueryWrapper(postgraduate, req.getParameterMap());
		Page<Postgraduate> page = new Page<Postgraduate>(pageNo, pageSize);
		IPage<Postgraduate> pageList = postgraduateService.page(page, queryWrapper);
		return Result.OK(pageList);

	}

	/**
	 *   添加
	 *
	 * @param postgraduate
	 * @return
	 */
	@AutoLog(value = "postgraduate-添加")
	@ApiOperation(value="postgraduate-添加", notes="postgraduate-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody Postgraduate postgraduate) {
		postgraduateService.save(postgraduate);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param postgraduate
	 * @return
	 */
	@AutoLog(value = "postgraduate-编辑")
	@ApiOperation(value="postgraduate-编辑", notes="postgraduate-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody Postgraduate postgraduate) {
		postgraduateService.updateById(postgraduate);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "postgraduate-通过id删除")
	@ApiOperation(value="postgraduate-通过id删除", notes="postgraduate-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		postgraduateService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "postgraduate-批量删除")
	@ApiOperation(value="postgraduate-批量删除", notes="postgraduate-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.postgraduateService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "postgraduate-通过id查询")
	@ApiOperation(value="postgraduate-通过id查询", notes="postgraduate-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Postgraduate postgraduate = postgraduateService.getById(id);
		if(postgraduate==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(postgraduate);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param postgraduate
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Postgraduate postgraduate) {
        return super.exportXls(request, postgraduate, Postgraduate.class, "postgraduate");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Postgraduate.class);
    }
//
//
//
//	 /**
//	  * 查询分类
//	  *
//	  * @param postgraduate
//	  * @param pageNo
//	  * @param pageSize
//	  * @param req
//	  * @return
//	  */
//	 @AutoLog(value = "postgraduate-查询分类")
//	 @ApiOperation(value="postgraduate-查询分类", notes="postgraduate-查询分类")
//	 @GetMapping(value = "/studyWork")
//	 public Result<?> queryByColleage(Postgraduate postgraduate,
//									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
//									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
//									HttpServletRequest req) {
//		 Map<String, String[]> parameterMap = req.getParameterMap();
//		 String[] colleages = parameterMap.get("colleage");
//		 Map<String, String[]> chooseLoca = new HashMap<>();
//		 chooseLoca.put("colleage",colleages);
//		 QueryWrapper<Postgraduate> queryWrapper = QueryGenerator.initQueryWrapper(postgraduate,chooseLoca);
//
//		 Page<Postgraduate> page = new Page<Postgraduate>(pageNo, pageSize);
//		 IPage<Postgraduate> pageList = postgraduateService.page(page, queryWrapper);
//		 return Result.OK(pageList);
//	 }

//	 /**
//	  * 考研学校分页列表查询
//	  *
//	  * @param postgraduate
//	  * @param pageNo
//	  * @param pageSize
//	  * @param req
//	  * @return
//	  */
//	 @AutoLog(value = "postgraduate-考研学校分页列表查询")
//	 @ApiOperation(value="postgraduate-考研学校分页列表查询", notes="postgraduate-考研学校分页列表查询")
//	 @PostMapping(value = "/graduate")
//	 public Result<?> Postgraduate(Postgraduate postgraduate,
//									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
//									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
////								    @RequestParam(name="id",required=true) String id,
//									HttpServletRequest req) {
//		 QueryWrapper<Postgraduate> queryWrapper = QueryGenerator.initQueryWrapper(postgraduate, req.getParameterMap());
//		 Page<Postgraduate> page = new Page<Postgraduate>(pageNo, pageSize);
//		 IPage<Postgraduate> pageList = postgraduateService.page(page, queryWrapper);
//		 return Result.OK(pageList);
//	 }


	 /**
	  * 通过college查询学校列表
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "查询学校列表")
	 @ApiOperation(value="查询学校列表", notes="查询学校列表")
	 @PostMapping(value = "/getSchoolList")
	 public Result<?> listByCollege(@RequestParam(name="college",required=true) String college,
									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {

		 LambdaQueryWrapper<Postgraduate> wrapper = new LambdaQueryWrapper<>();
		 wrapper.eq(Postgraduate::getColleage,college)
				 .eq(Postgraduate::getStudyWork,"升学")
				 .groupBy(Postgraduate::getRegSignDept);
		 List<Postgraduate> list = postgraduateService.list(wrapper);
		 Page<Postgraduate> page = new Page<Postgraduate>(pageNo, pageSize);
		 IPage<Postgraduate> pageList = postgraduateService.page(page, wrapper);
//		 js= JSON.parseArray(list);
//		 Map<String,Object> collMap = new HashMap<>();
//		 collMap.put("colleage",college);
//		 List<Postgraduate> postgraduates = postgraduateService.listByMap(collMap);
//		 HashSet<Map<String,Object>> easyPostgraduates = new HashSet<>();
//		 for (int size = 0;size<postgraduates.size();size++){
//			 Map<String,Object> map = new HashMap<>();
//			 if(postgraduates.get(size).getStudyWork().equals("升学")){
//
////				 easyPostgraduates.add(postgraduates.get(size).getRegSignDept());
//			 	map.put("regSignDept",postgraduates.get(size).getRegSignDept()) ;
//				 easyPostgraduates.add(map);
//			 }
//		 }
		 return Result.OK(pageList);

	 }


	 /**
	  * 通过学校和学院查询学生列表
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "查询学生列表")
	 @ApiOperation(value="查询学生列表", notes="查询学生列表")
	 @PostMapping(value = "/getStudentList")
	 public Result<?> listBySchool(@RequestParam(name="school",required=true) String school,
								   @RequestParam(name="college",required=true) String college,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		 QueryWrapper<Postgraduate> query = new QueryWrapper<>();
		 query.like("reg_sign_dept",school);
		 query.like("colleage",college);
//		 query.eq("empl_status","升学");
		 Page<Postgraduate> page = new Page<Postgraduate>(pageNo,  pageSize);
		 IPage<Postgraduate> pageList = postgraduateService.page(page, query);
		 if(pageList == null){
			 return Result.error("没有找到报考此学校的人员");
		 }
		 return Result.OK(pageList);
	 }

	 /**
	  * 通过学校和学院查询学生列表
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "搜索学校列表")
	 @ApiOperation(value="搜索学校列表", notes="搜索学校列表")
	 @PostMapping(value = "/searchSchoolList")
	 public Result<?> searchSchoolList(@RequestParam(name="search",required=true) String search,
								  	   @RequestParam(name="college",required=true) String college,
									   @RequestParam(name="gradenum",required=true) String gradenum
//								       @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
//									   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize
	 								) {
		 LambdaQueryWrapper<Postgraduate> queryWrapper = new LambdaQueryWrapper<>();
		 queryWrapper.eq(Postgraduate::getColleage,college)
				 		.eq(Postgraduate::getGradenum,gradenum)
				     .eq(Postgraduate::getEmplStatus,"升学")
				     .like(Postgraduate::getRegSignDept,search)
		 			 .groupBy(Postgraduate::getRegSignDept);
//		 Page<Postgraduate> page = new Page<Postgraduate>(pageNo, pageSize);
//		 IPage<Postgraduate> pageList = postgraduateService.page(page, queryWrapper);
		 List<Postgraduate> list = postgraduateService.list(queryWrapper);
		 if(list == null){
			 return Result.error("未找到该学校！");
		 }
		 return Result.OK(list);
	 }


 }
